package com.practice.LocalDateTime和ZonedDateTime等java8中全新的时间API相关操作;
import java.time.*;

/**
 * @program: tenghw-practice-demo
 * @author: tenghw
 * @create: 2024-01-14 17:43
 * @Version 1.0
 * @description:
 **/
public class Demo3 {
    public static void main(String[] args) {
        // java7及之前获取时间戳的方式
        long timeMillis = System.currentTimeMillis();
        System.out.println(timeMillis);

        // java8+获取时间戳的方式
        Instant instant = Instant.now();//默认UTC时区（平时用的获取毫秒，用这个就好）；获取时间戳：long timeMillis1=Instant.now().toEpochMilli();
        System.out.println(instant);
        // 注意：并不会改变时间本身，它所做的实际上是将当前UTC时间转换到上海时区并立即又转换回UTC时间
        Instant instant1 = Instant.now().atZone(ZoneId.of("Asia/Shanghai")).toInstant();
        System.out.println(instant1);
        ZonedDateTime shanghaiTime = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println("上海时区时间: " + shanghaiTime);

        long timeMillis1 = instant.toEpochMilli();
        System.out.println(timeMillis1);
        long timeMillis2 = instant1.toEpochMilli();
        System.out.println(timeMillis2);

        // 带偏移量的运算
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);
    }
}